package user

import (
	"gq_volunteer_platform/bean"
	"gq_volunteer_platform/model/user"

	"github.com/gin-gonic/gin"
)

func MyPointRecordList(c *gin.Context) {

	var req struct {
		Page  int `binding:"required,gte=1,max=10000000"`
		Limit int `binding:"required,gte=1,max=10000000"`
	}

	if err := c.ShouldBindQuery(&req); err != nil {
		bean.Log.Warning(err.Error())
		c.String(400, err.Error())
		return
	}

	var offset = (req.Page - 1) * req.Limit

	// 获取登录用户对象
	var u user.User
	if err := u.Get(c); err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	var count int64
	var list []user.UserPointRecord

	var tx = bean.MysqlDb.Model(&user.UserPointRecord{}).Where("`user_id`=?", u.ID)

	// 查询总数
	if err := tx.Count(&count).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	// 查询列表
	if err := tx.Limit(req.Limit).Offset(offset).Order("`id` DESC").Find(&list).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	c.PureJSON(200, gin.H{
		"count": count,
		"list":  list,
	})

}
